package fr.univsavoie.tatiana2dvisualisation;

import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.Polyline;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;

public class DrawCondition {
	String facetvalue;
	Color pastcolor;
	Color futurecolor;
	Color presentcolor;
	private int figuretype = 0;
	private Integer minx;
	private Integer miny;
	private Integer maxx;
	private Integer maxy;
	public final static int POINT = 0;
	public final static int DISK = 1;
	public final static int PAST = 0;
	public final static int PRESENT = 1;
	public final static int FUTURE = 2;
	public static final int CURRENT = 3;


	public DrawCondition(String facetvalue, Color pastcolor,
			Color futurecolor, Color presentcolor) {
		super();

		this.facetvalue = facetvalue;
		this.pastcolor = pastcolor;
		this.futurecolor = futurecolor;
		this.presentcolor = presentcolor;

	}

	public void setCurrentShape(int i) {
		figuretype = i;
	}

	public boolean meetsCondition(EyeTrackerReplayableEvent event) {
		return (facetvalue==null || event.type.equals(facetvalue));
	}

	public boolean willDraw(EyeTrackerReplayableEvent event, EyeTrackerReplayableEvent prev, int timetype) {
		if (meetsCondition(event)) {
			
			if (event.pressure==0 || prev.pressure==0) return false;
			
			if (timetype==FUTURE) {
				if (futurecolor!=null) return true;
			} else if (timetype==PRESENT || timetype==CURRENT) {
				if (presentcolor!=null) return true;
			} else {
				if (pastcolor!=null) return true;
			}
		}
		return false;
	}

	public IFigure draw(EyeTrackerReplayableEvent current, EyeTrackerReplayableEvent prev, int timetype) {
		IFigure ret = null;
		if (willDraw(current,prev,timetype)) {
			int x1 = current.x/10-(minx/10)+20;
			int y1 = current.y/10-(miny/10)+20;
			int x2 = prev.x/10-(minx/10)+20;
			int y2 = prev.y/10-(miny/10)+20;


			Polyline line = new Polyline();
			if (timetype==FUTURE) {
				line.setForegroundColor(futurecolor);
			} else if (timetype==PRESENT || timetype==CURRENT) {
				line.setForegroundColor(presentcolor);
				if (timetype==PRESENT && figuretype==DISK) {
					Ellipse e = new Ellipse();
					e.setBackgroundColor(presentcolor);
					e.setBorder(new LineBorder(presentcolor));
					e.setBounds(new Rectangle(x1-3,y1-3,6,6));
					return e;
				}
			} else {
				line.setForegroundColor(pastcolor);
			}
			line.addPoint(new Point(x1,y1));
			line.addPoint(new Point(x2,y2));
			ret = line;	
		}
	return ret;
	}

	public void setBounds(Integer minx, Integer maxx, Integer miny, Integer maxy) {
		this.minx=minx;
		this.miny=miny;
		this.maxx=maxx;
		this.maxy=maxy;
		
	}
}